const {ccclass, property} = cc._decorator;

/**
 * 每一个任务项目
 */
@ccclass
export default class TaskItem extends cc.Component {

    id: number

    @property(cc.Label)
    titleLabel: cc.Label;

    @property(cc.Label)
    descLabel: cc.Label;

    @property(cc.Node)
    isCompleted: cc.Node;

    @property(cc.Node)
    btn: cc.Node;

    init(data: any = {}) {
        this.id = data.id
        this.titleLabel.string = data.name
        this.descLabel.string = data.desc
        const prefix = data.key === 'sign' ? '' : '去'
        const btnStr = data.btnStr || data.name
        cc.find('Background/Label', this.btn).getComponent(cc.Label).string = `${prefix}${btnStr}`
        if (data.is_finish) {
            this.btn.active = false
            return this.isCompleted.active = true
        } else {
            this.isCompleted.active = false
            this.btn.active = true
        }
        if (typeof data.onClick === "function") {
            this.btn.on(cc.Node.EventType.TOUCH_START, data.onClick.bind(data.target || this, this.id, data.key))
        }
    }
}
